def threshold_cluster(Data_set, threshold):
    """
    利用数据差值进行聚类
    输入参数Data_set为输入的数据集，可以为列表、数组、Series、DataFrame。threshold为数据大小分类的门限值。
    输出class_k为数据归类列表集合，index_list为数据归类对应的索引集合。
    """
    # 统一格式化数据为一维数组
    stand_array = np.asarray(Data_set).ravel('C')
    stand_Data = Series(stand_array)
    index_list, class_k = [], []
    while stand_Data.any():
        if len(stand_Data) == 1:
            index_list.append(list(stand_Data.index))
            class_k.append(list(stand_Data))
            stand_Data = stand_Data.drop(stand_Data.index)
        else:
            class_data_index = stand_Data.index[0]
            class_data = stand_Data[class_data_index]
            stand_Data = stand_Data.drop(class_data_index)
            if (abs(stand_Data - class_data) <= threshold).any():
                args_data = stand_Data[abs(stand_Data - class_data) <= threshold]
                stand_Data = stand_Data.drop(args_data.index)
                index_list.append([class_data_index] + list(args_data.index))
                class_k.append([class_data] + list(args_data))
            else:
                index_list.append([class_data_index])
                class_k.append([class_data])
    return index_list, class_k


import numpy as np

from pandas import Series, DataFrame

Data_set = [0.691942974, 0.691942974, 0.664522549, 0.719363399,
            0.697202659, 0.664522549, 0.637102124, 0.719363399,
            0.691942974, 0.691942974, 0.719363399, 0.637102124,
            0.664522549, 0.719363399, 0.637102124, 0.609681699,
            0.637102124, 0.664522549]
Max,Min = max(Data_set),min(Data_set)
threshold = (Max-Min)/5
print(threshold)
index_list, class_k = threshold_cluster(Data_set, threshold)

print(class_k,index_list)
